---
title: "Javascript Snippets"
date: 2017
categories:
- js
tags:
---

<div id="content">
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org31258bf">Javascript Snipets</a>
<ul>
<li><a href="#parse-asp-time">parse asp time</a></li>
<li><a href="#html-去除标签">html 去除标签</a></li>
<li><a href="#create-dom">create dom</a></li>
<li><a href="#邮件正则表达式">邮件正则表达式</a></li>
<li><a href="#获取ip需要外部服务器支持">获取IP（需要外部服务器支持）</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="outline-2" id="outline-container-org31258bf">
<h2 id="org31258bf">Javascript Snipets</h2>
<div class="outline-text-2" id="text-org31258bf">
</div>
<div class="outline-3" id="outline-container-orge04a6d2">
<h3 id="parse-asp-time"><a id="orge04a6d2"></a>parse asp time</h3>
<div class="outline-text-3" id="text-parse-asp-time">
<pre class="example">
function ParseAspnetTime(rawTime) {
        return eval('(' + rawTime.replace(/\/Date\((\d+)\)\//gi, "new Date($1)") + ')');
}
function getTimeStr_1(time) {
    return time.getFullYear() + '-' + (time.getMonth() + 1) + '-' + time.getDate();
}
function getTimeStr_2(time) {
    return time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds();
}
var time = eval('(' + rawTime.replace(/\/Date\((\d+)\)\//gi, "new Date($1)") + ')');
return time.getFullYear() + '-' + (time.getMonth()+ 1) + '-' + time.getDay() + ' '
        +time.getHours() + ":"+time.getMinutes()+":"+time.getSeconds();
</pre>
</div>
</div>
<div class="outline-3" id="outline-container-orgae8aa65">
<h3 id="html-去除标签"><a id="orgae8aa65"></a>html 去除标签</h3>
<div class="outline-text-3" id="text-html-去除标签">
<pre class="example">
function extraHTML(str) {
    str = str.replace(/&lt;(img)[^&gt;]*&gt;|&lt;\/(img)&gt;/g, '');
    str = str.replace(/&lt;(table|tbody|tr|td|th|)[^&gt;]*&gt;|&lt;\/(table|tbody|tr|td|th|)&gt;/g, '');
    str = str.replace(/&lt;(div|blockquote|fieldset|legend)[^&gt;]*&gt;|&lt;\/(div|blockquote|fieldset|legend)&gt;/g, '');
    str = str.replace(/&lt;(font|i|u|h[1-9]|s)[^&gt;]*&gt;|&lt;\/(font|i|u|h[1-9]|s)&gt;/g, '');
    str = str.replace(/&lt;(style|strong)[^&gt;]*&gt;|&lt;\/(style|strong)&gt;/g, '');
    str = str.replace(/&lt;a[^&gt;]*&gt;|&lt;\/a&gt;/g, '');
    str = str.replace(/&lt;(meta|iframe|frame|span|tbody|layer)[^&gt;]*&gt;|&lt;\/(iframe|frame|meta|span|tbody|layer)&gt;/g, '');
    str = str.replace(/&lt;a[^&gt;]*/g, '');
    return str;
}
</pre>
</div>
</div>
<div class="outline-3" id="outline-container-org6ab6eee">
<h3 id="create-dom"><a id="org6ab6eee"></a>create dom</h3>
<div class="outline-text-3" id="text-create-dom">
<pre class="example">
var CWndBase = function () {
    /*
    Name:element's name
    Attributes:element's attributes
    Text:text content in node
    Style:apply style to the dom.
    Dom:If dom is speified,then append dom instead of creating.
    Childs:child nodes
    */
    this.createDom = function (domDataObj) {
        var dom;
        if (domDataObj.Dom) {
            dom = domDataObj.Dom;
        }
        else {
            dom = document.createElement(domDataObj.Name);
            if (domDataObj.Text) {
                dom.innerHTML = domDataObj.Text;
            }
            if (domDataObj.Style) {
                for (var s in domDataObj.Style) {
                    dom.style[s] = domDataObj.Style[s];
                }
            }
        }
        if (domDataObj.Attributes) {
            for (var attr in domDataObj.Attributes) {
                var value = domDataObj.Attributes[attr]
                dom[attr] = value;
            }
        }
        if (domDataObj.Childs) {
            for (var i = 0; i &lt; domDataObj.Childs.length; i++) {
                dom.appendChild(this.createDom(domDataObj.Childs[i]));
            }
        }
        return dom;
    }
    /*
    2-dimension array of:{}
    Attributes:element's attributes
    Text:text content in node
    Childs:child nodes
    */
    this.createTable = function (tableObj) {
        var table = document.createElement('table');
        for (var i = 0; i &lt; tableObj.length; i++) {
            var row = table.insertRow();
            for (var j = 0; j &lt; tableObj[i].length; j++) {
                var cell = row.insertCell();
                var data = tableObj[i][j];
                if (data.Text) {
                    cell.innerHTML = data.Text;
                }
                if (data.Attributes) {
                    for (var prop in data.Attributes) {
                        cell[prop] = data.Attributes[prop];
                    }
                }
                if (data.Childs) {
                    for (var k = 0; k &lt; data.Childs.length; k++) {
                        cell.appendChild(this.createDom(data.Childs[k]));
                    }
                }
            }
        }
        return table;
    }

    this.geneObjFromDom = function(id){
        return geneObjFromDomProc(id);
    }

    function geneObjFromDomProc(id){
        var retn = {};
        var dom = document.getElementById(id);
        var nodeName = dom.nodeName;
        if(nodeName === 'table'){

        }
        else{
            retn.Name = nodeName;
            retn.Styles = dom.style;
            retn.Text = dom.innerText;
            retn.Attributes = {};            
            for(var i = 0;i&lt;dom.attributes.length;i++){
                retn.Attributes[dom.attributes[i].name] = dom.attributes[i].value;
            }
            retn.Childs = [];
            for(var i = 0;i&lt;dom.childElementCount;i++){
                retn.Childs.push(this.geneObjFromDom(dom.childNodes[i]));
            }
        }
    }
}
</pre>
</div>
</div>
<div class="outline-3" id="outline-container-orgfbf817a">
<h3 id="邮件正则表达式"><a id="orgfbf817a"></a>邮件正则表达式</h3>
<div class="outline-text-3" id="text-邮件正则表达式">
<pre class="example">
var Regex = /^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/;
Regex.test('gdme1320.sldkf@qq.com');
</pre>
<pre class="example">
var Regex = /^[\w-]+(\.[\w-]+)*@[\w-]+((\.[\w-]+)+)$/;  
var Regex = /([\w-]{1,})@([\w-]{1,}\.)(\w.{1,})/;
Regex.test('123@as.ki-d.com');
</pre>
<pre class="example">
//摘自微软demo https://msdn.microsoft.com/zh-cn/library/938d9dz2%28v=vs.90%29.aspx#
Regex.IsMatch(email, @"^([\w-]+\.)*?[\w-]+@[\w-]+\.([\w-]+\.)*?[\w]+$");
</pre>
</div>
</div>
<div class="outline-3" id="outline-container-orga24d918">
<h3 id="获取ip需要外部服务器支持"><a id="orga24d918"></a>获取IP（需要外部服务器支持）</h3>
<div class="outline-text-3" id="text-获取ip需要外部服务器支持">
<pre class="example">
var uip = '8.8.8.8';
$.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&amp;ip='+uip, function(_result){
    var ipData = ""; //初始化保存内容变量
    if (remote_ip_info.ret == '1'){
        ipData += "IP 详细信息：&lt;br&gt;";
        ipData += "IP：" + uip + "&lt;br&gt;";
        ipData += "国家：" + remote_ip_info.country + "&lt;br&gt;";
        ipData += "省份：" + remote_ip_info.province + "&lt;br&gt;";
        ipData += "城市：" + remote_ip_info.city + "&lt;br&gt;";
        ipData += "区：" + remote_ip_info.district + "&lt;br&gt;";
        ipData += "ISP：" + remote_ip_info.isp + "&lt;br&gt;";
        ipData += "类型：" + remote_ip_info.type + "&lt;br&gt;";
        ipData += "其他：" + remote_ip_info.desc + "&lt;br&gt;";
        $("#sina_ip_info").html(ipData); //显示处理后的数据
    } else {
        alert('错误', '没有找到匹配的 IP 地址信息！');
    }
});

jQuery(function($){  
   var url = 'http://chaxun.1616.net/s.php?type=ip&amp;output=json&amp;callback=?&amp;_=' + Math.random();
    $.getJSON(url, function (data) {
        alert("a");
        alert(JSON.stringify(data));
       $(".position-text").text(data.Isp);
          });  
});
</pre>
</div>
</div>
</div>
</div>
<div class="status" id="postamble">
<p class="date">Date: 2017</p>
<p class="author">Author: gdme1320</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
